package com.wymobilearchitecture

import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.View
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.fragment.app.FragmentManager
import com.example.w_library.utils.DisplayUtil
import com.wy.common.tab.WFragmentTabView
import com.wy.common.tab.WTabViewAdapter
import com.wy.w_ui.tab.bottom.WTabBottom
import com.wy.w_ui.tab.bottom.WTabBottomInfo
import com.wy.w_ui.tab.bottom.WTabBottomLayout
import com.wy.w_ui.tab.common.IWTabLayout
import com.wymobilearchitecture.home.*

/**
 *  MainActivity 内的一些逻辑
 */
open class MainActivityLogic(private var activityProvider: ActivityProvider, savedInstanceState: Bundle?) {

    companion object{
        const val SAVED_CURRENT_ID : String = "SAVED_CURRENT_ID"
    }

    lateinit var fragmentTabView : WFragmentTabView
    lateinit var   bottomTabLayout : WTabBottomLayout
    lateinit var infoList : MutableList<WTabBottomInfo<*>>
    var currentItemIndex : Int = 0


    init {
        //不保留活动导致fragment重叠问题
        if (savedInstanceState != null){
            currentItemIndex = savedInstanceState.getInt(Companion.SAVED_CURRENT_ID)
        }
        initTabBottomLayout()
    }


    /**
     * 初始化 FragmentTabView
     */
    fun initFragmentTabView(){

        val tabViewAdapter : WTabViewAdapter = WTabViewAdapter(activityProvider.getSupportFragmentManager(),infoList)
        fragmentTabView = activityProvider.findViewById(R.id.fragment_tab_view)
        fragmentTabView.adapter = tabViewAdapter

        //判断是否初始化
        if (::fragmentTabView.isInitialized){

        }
    }


    /**
     * 初始化 tabBottomLayout
     */
    fun initTabBottomLayout(){

        bottomTabLayout = activityProvider.findViewById(R.id.tab_bottom_layout)
        bottomTabLayout.alpha = 0.85f
        infoList = ArrayList()

        val defaultColor : Int? = activityProvider.getResources().getColor(R.color.tabBottomDefaultColor)
        val tintColor : Int? = activityProvider.getResources().getColor(R.color.tabBottomTintColor)


        val home1  = WTabBottomInfo(
            "首页",
            "fonts/iconfont.ttf",
            activityProvider.getString(R.string.if_home_icon),
            activityProvider.getString(R.string.if_home_icon),
            defaultColor,
            tintColor,
            WTabBottomInfo.TabType.ICON
        )
        home1.fragment = HomeFragment01::class.java


        val home2  = WTabBottomInfo(
            "首页",
            "fonts/iconfont.ttf",
            activityProvider.getString(R.string.if_recommend),
            activityProvider.getString(R.string.if_recommend),
            defaultColor,
            tintColor,
            WTabBottomInfo.TabType.ICON
        )
        home2.fragment = HomeFragment02::class.java


//        val home3  = WTabBottomInfo(
//            "首页",
//            "fonts/iconfont.ttf",
//            getString(R.string.if_chat),
//            getString(R.string.if_chat),
//            "#ff656667",
//            "#ffd44949",
//            WTabBottomInfo.TabType.ICON
//        )

        val bitmap: Bitmap = BitmapFactory.decodeResource(activityProvider.getResources(),R.mipmap.ic_launcher,null)

        val home3 = WTabBottomInfo<String>(
            "icon",
            bitmap,bitmap
        )
        home3.fragment = HomeFragment03::class.java


        val home4  = WTabBottomInfo(
            "首页",
            "fonts/iconfont.ttf",
            activityProvider.getString(R.string.if_profile),
            activityProvider.getString(R.string.if_profile),
            defaultColor,
            tintColor,
            WTabBottomInfo.TabType.ICON
        )
        home4.fragment = HomeFragment04::class.java


        val home5  = WTabBottomInfo(
            "首页",
            "fonts/iconfont.ttf",
            activityProvider.getString(R.string.if_category),
            activityProvider.getString(R.string.if_category),
            defaultColor,
            tintColor,
            WTabBottomInfo.TabType.ICON
        )
        home5.fragment = HomeFragment05::class.java

        infoList.add(home1)
        infoList.add(home2)
        infoList.add(home3)
        infoList.add(home4)
        infoList.add(home5)

        bottomTabLayout.inflateInfo(infoList)

//        tabBottomLayout.addTabSelectChangeListener{
//                _, _, nextInfo -> Toast.makeText(this@TabBottomActivity,nextInfo.name,Toast.LENGTH_SHORT).show()
//        }

        initFragmentTabView()

        bottomTabLayout.addTabSelectChangeListener(object : IWTabLayout.OnTabSelectListener<WTabBottomInfo<*>>{
            override fun onTabSelectChange(
                index: Int,
                prevInfo: WTabBottomInfo<*>?,
                nextInfo: WTabBottomInfo<*>
            ) {
                fragmentTabView.currentItem = index
                this@MainActivityLogic.currentItemIndex = index
            }
        })

        bottomTabLayout.defaultSelected(infoList[this@MainActivityLogic.currentItemIndex])

        val tab3 : WTabBottom? = bottomTabLayout.findTab(home3)

        tab3?.apply { restHeight(DisplayUtil.dp2Px(context,66)) }
    }

    /**
     * 不需要持有 MainActivity 对像,持有 ActivityProvider 方便解耦
     */
     interface ActivityProvider{

        fun <T : View> findViewById(@IdRes id : Int) : T

        fun getResources() : Resources

        fun getSupportFragmentManager() : FragmentManager

        fun getString(@StringRes resId : Int) : String
    }


    /**
     * 处理当应用被回收之后 Activity 重新加载 Fragment 重复初始化问题
     */
     fun onSaveInstanceState(outState: Bundle) {
        outState.putInt(Companion.SAVED_CURRENT_ID,currentItemIndex)
    }
}